package com.xxq.gulimall.product.controller;

import java.util.Arrays;
import java.util.Map;

// // import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xxq.common.valid.AddGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xxq.gulimall.product.entity.AttrEntity;
import com.xxq.gulimall.product.service.AttrService;
import com.xxq.common.utils.PageUtils;
import com.xxq.common.utils.R;



/**
 * 商品属性
 *
 * @author xxq
 * @email 1607903138@qq.com
 * @date 2022-04-28 20:20:14
 */
@Slf4j
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // // // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     *  05、 获取分类规格参数
     */
    @RequestMapping("/base/list/{catelogId}")
    // // // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){

        PageUtils page = attrService.queryPageByCatelogId(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     *  09、 获取分类销售属性
     */
    @RequestMapping("/sale/list/{catelogId}")
    // // // @RequiresPermissions("product:attr:list")
    public R listSale(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrService.queryPageByCatelogId(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     *  07、 查询属性详情
     */
    @RequestMapping("/info/{attrId}")
    // // // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getByIdWithCatelogPath(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     *  06、 保存属性【规格参数，销售属性】
     */
    @RequestMapping("/save")
    // // // @RequiresPermissions("product:attr:save")
    public R save(@Validated(AddGroup.class) @RequestBody AttrEntity attr){
		attrService.saveOrUpdateWithAttrGroupId(attr, "save");

        return R.ok();
    }

    /**
     *  08、 修改属性
     */
    @RequestMapping("/update")
    // // // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrEntity attr){
        attrService.saveOrUpdateWithAttrGroupId(attr, "update");

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // // // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
